<?php

namespace app\api\controller\user;

use app\api\controller\Controller;
use app\api\model\User as UserModel;
use app\api\model\Order as OrderModel;
use app\api\model\Setting as SettingModel;
use app\api\model\UserCoupon;
use app\api\model\WxappMenu;
use app\common\model\Setting;

/**
 * 个人中心主页
 * Class Index
 * @package app\api\controller\user
 */
class Index extends Controller
{
    /**
     * 获取当前用户信息
     * @return array
     * @throws \app\common\exception\BaseException
     * @throws \think\Exception
     * @throws \think\exception\DbException
     */
    public function detail(UserCoupon $userCoupon)
    {
        // 当前用户信息
        $user = $this->getUser(false);
        $coupon_count = $userCoupon->getCount($user ? $user['user_id'] : 0);
        if ($coupon_count) {
            $user['coupon_count'] = $coupon_count;
        }
        // 订单总数
        $model = new OrderModel;
        return $this->renderSuccess([
            'userInfo' => $user,
            'orderCount' => [
                //待付款
                'payment' => $model->getCount($user, 'payment'),
                //待发货
                'delivery' => $model->getCount($user, 'delivery'),
                //待收货
                'received' => $model->getCount($user, 'received'),
                //待评价
                'comment' => $model->getCount($user, 'comment'),
            ],
            // 个人中心菜单列表
            'menus' => WxappMenu::getMenus(),
            //开关我的钱包
            'is_recharge' => SettingModel::getItem('trade')['is_recharge']
        ]);
    }

}
